package notification

import (
	"errors"
	"fmt"
	request2 "github.com/flipped-aurora/gin-vue-admin/server/model/im/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/notification/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service/im"
	"github.com/gin-gonic/gin"
)

type MessageService struct {
}

// SendMessage 发送系统通知消息
func (m MessageService) SendMessage(ctx *gin.Context, req request.SendMessageRequest) error {
	if !req.IsSendAll && len(req.RecvIDs) <= 0 {
		return errors.New("empty receiver")
	}

	var sendReq = request2.IMSendNotificationMsgRequest{
		RecvIDs:        req.RecvIDs,
		SendID:         req.SendID,
		SenderNickname: req.SenderNickname,
		SenderFaceURL:  req.SenderFaceURL,
		Content: request2.MsgContent{
			NotificationName: req.SenderNickname,
			NotificationType: 1,
			Text:             req.Text,
			MixType:          0,
		},
		ContentType: req.ContentType,
		SessionType: req.SessionType,
		IsSendAll:   req.IsSendAll,
	}

	// 发送系统通知消息
	var errSend = im.IMNotificationServiceApp.SendNotification(ctx, sendReq)
	if errSend != nil {
		return fmt.Errorf("recv_id=%s||sender_id=%s||msg=send_notification_failed||err=%w", sendReq.RecvIDs, sendReq.SendID, errSend)
	}
	return nil
}
